// PersistentObject.cpp: implementation of the PersistentObject class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PersistentObject.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UE_IMPLEMENT_DYNAMIC(Persistable, SmallObject);
Persistable::Persistable()
{
	m_eState = eCREATE;
	m_eOpenModel = UeDb::eForWrite;
	m_nReaders = 1;
}

Persistable::~Persistable()
{

}

Persistable::Persistable( const Persistable& obj )
{
}

UESoft::ErrorStatus Persistable::Close()
{
	if ( m_nReaders == 0 )
	{
		return UESoft::eNotOpenForRead;
	}
	m_nReaders--;
	return UESoft::eOk;
}

UESoft::ErrorStatus Persistable::Erase()
{
	return SetDelete();
}


void Persistable::AssertReadEnable() const
{
	if ( IsReadEnable() ) 
		return;
	ASSERT( FALSE );
	throw UESoft::eNotOpenForRead;
}

void Persistable::AssertWriteEnable()
{
	if ( IsWriteEnable() )
	{
		if(m_eState == eREAD)
			m_eState = eMODIFY;
		return;
	}
	ASSERT( FALSE );
	throw UESoft::eNotOpenForWrite;
}

UESoft::ErrorStatus Persistable::Open( UeDb::OpenModel om)
{
	if ( IsWriteEnable() )
		return UESoft::eWasOpenForWrite;
	else if ( IsReadEnable() && om == UeDb::eForWrite )
		return UESoft::eWasOpenForRead;

	m_nReaders++;
	m_eOpenModel = om;
	return UESoft::eOk;
}